package com.itheima.manager_system.controller;

import com.itheima.manager_system.dao.StudentDao;
import com.itheima.manager_system.domain.Student;
import com.itheima.manager_system.service.StudentService;

import java.util.Scanner;

public class StudentController {
    private Scanner sc = new Scanner(System.in);
    private StudentDao studentDao = new StudentDao();
    private StudentService studentService = new StudentService();

    public void studentEntry() {

//        StudentController studentController = new StudentController();
        while (true) {
            System.out.println("1 增加学生信息 2 删除学生信息 3 更新学生信息 4 查询所有学生 5 返回上一层 6 退出");
            switch (sc.next()) {
                case "1":
//                    System.out.println("增");
                    addStudent();
                    break;
                case "2":
//                    System.out.println("删");
                    removeStudent();//todo
                    break;
                case "3":
//                    System.out.println("改");

                    updateStudent();//todo
                    break;
                case "4":
//                    System.out.println("查");
                    AllStudents();
                    break;
                case "5":
//                    System.out.println("返回上一层");
                    return;
                case "6":
                    System.out.println("谢谢使用再见");
                    System.exit(0);
                    break;
                default:
                    System.out.println("输入有误");
                    break;


            }
        }


    }


    //查
    public void AllStudents() {
        studentDao.AllStudents();
    }


    //改
    public void updateStudent() {
        if (studentService.isEmpty(studentDao.getList())) {
            System.out.println("暂无可供修改信息");
            return;
        }
        System.out.println("请输入要修改的编号");
        String updateId = sc.next();
        boolean b = studentService.isExist(updateId);
        if(!b){
            studentDao.updateStudent(updateId);
        }else{
            System.out.println(updateId+" 编号不存在");
        }

    }

    //删
    public void removeStudent() {
        if (studentService.isEmpty(studentDao.getList())) {
            System.out.println("暂无可供查询信息");
            return;
        }

        System.out.println("请输入要删除的编号");
        String removeId = sc.next();

        boolean b = studentService.isExist(removeId);
        if (b) {
            System.out.println(removeId + " 编号不存在");
        } else {
            studentDao.removeStudent(removeId);
        }


    }

    //增
    public void addStudent() {
        studentDao.addStudent();
    }


    public Student inputStudent() {
//        StudentService studentService = new StudentService();
        Student st = new Student();
        String addId = null;
        while (true) {
            System.out.println("请输入编号:");
            addId = sc.next();
            boolean isExist = studentService.isExist(addId);
            if (!isExist) {
                System.out.println(addId + " 编号已存在");
            } else {
                break;
            }
        }
        System.out.println("请输入名字:");
        String addName = sc.next();
        System.out.println("请输入年龄:");
        int addAge = sc.nextInt();
        System.out.println("请输入出生日期:");
        String addBirthday = sc.next();
        st.setId(addId);
        st.setName(addName);
        st.setAge(addAge);
        st.setBirthday(addBirthday);
        return st;

    }
}
